package com.example.cnotebook.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.cnotebook.R;
import com.example.cnotebook.pojo.CategoryInfo;

import java.util.List;

public class CategoryGridViewAdapter extends BaseAdapter {

    private List<CategoryInfo> categorys;
    private LayoutInflater layoutInflater;

    public CategoryGridViewAdapter(Context context, List<CategoryInfo> categorys) {
        this.categorys = categorys;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return categorys.size();
    }

    @Override
    public Object getItem(int position) {
        return categorys.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_gridview_layout, null);
            holder = new ViewHolder();
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            holder.tv_detail = convertView.findViewById(R.id.tv_detail);
            holder.iv_category_img = convertView.findViewById(R.id.iv_category_img);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        CategoryInfo categoryInfo = categorys.get(position);
        holder.tv_name.setText(categoryInfo.getName());
        holder.tv_detail.setText(categoryInfo.getDetail());
        holder.iv_category_img.setImageResource(R.mipmap.a9);
        return convertView;
    }

    class ViewHolder {
        TextView tv_name, tv_detail;
        ImageView iv_category_img;
    }

}